package com.coocaa.x.service.litedaemon.daemons.tvpie.app.bean;

import com.alibaba.fastjson.JSON;
import com.coocaa.x.framework.data.JObject;
import com.coocaa.x.service.litedownloader.data.DownloadError;
import com.coocaa.x.service.litedownloader.data.DownloadExtra;
import com.coocaa.x.service.litedownloader.data.DownloadTask;
import com.coocaa.x.service.litedownloader.data.ProcessInfo;
import com.coocaa.x.service.litepm.data.AppInfo;

import org.json.JSONObject;

import java.util.List;
import java.util.UUID;

public class AppResultBean<T> extends JObject {

    public String id = UUID.randomUUID().toString();
    public T result;
    public String total;
    public String page;

    public static <E> List<E> parseArray(String json, Class<E> clazz) {
        Thread.currentThread().setContextClassLoader(clazz.getClassLoader());
        return JSON.parseArray(json, clazz);
    }

    public static <E> E parseObject(String json, Class<E> clazz) {
        Thread.currentThread().setContextClassLoader(clazz.getClassLoader());
        return JSON.parseObject(json, clazz);
    }

    public AppResultBean() {
    }

    ;

    public AppResultBean(String jString) {
        try {
            JSONObject js = new JSONObject(jString);
            String id = js.optString("id");
            this.id = id;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * ******************************* 安装模块 ***********************************************
     */
    public static class OnInstallstart extends JObject {
        public String apkfile;
        public String pkgname;

        public OnInstallstart() {
        }

        public OnInstallstart(String apkfile, String pkgname) {
            this.apkfile = apkfile;
            this.pkgname = pkgname;
        }

        ;

    }

    public static class OnInstalled extends JObject {
        public String apkfile;
        public String pkgname;
        public boolean success;
        public String extra;
        public String appname;

        public OnInstalled() {
            super();
        }

        public OnInstalled(String apkfile, String pkgname, boolean success,
                           String extra, String appname) {
            super();
            this.apkfile = apkfile;
            this.pkgname = pkgname;
            this.success = success;
            this.extra = extra;
            this.appname = appname;
        }

    }

    public static class OnUnInstalled extends JObject {
        public String pkgname;
        public boolean success;
        public String extra;

        public OnUnInstalled() {
        }

        public OnUnInstalled(String pkgname, boolean success, String extra) {
            this.pkgname = pkgname;
            this.success = success;
            this.extra = extra;
        }

    }

    /**
     * ********************************** 下载模块 ********************************************
     */

    public static class OnProcessInfo extends JObject {
        public String appid;
        public long current = 0L;
        public long length = 0L;
        public float speed = 0.0F;
        public String pkgName = "";

        public OnProcessInfo(DownloadTask task, ProcessInfo info) {
            this.current = info.current;
            this.length = info.length;
            this.appid = task.ID;
            this.speed = info.speed;
            try {
                DownloadExtra extra = AppResultBean.parseObject(task.extra,
                        DownloadExtra.class);
                this.pkgName = extra.pkgname;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

    public static class onError extends JObject {
        public DownloadTask task;
        public DownloadError error;

        public onError(DownloadTask task, DownloadError error) {
            super();
            this.task = task;
            this.error = error;
        }

    }

    public static class GetInstalled extends JObject {
        public String appName;
        public boolean hasUpdate;
        public String pkgName;
        public String versionName = "";
        public int versionCode;
        public int usedTimes;
        public int flag;
        public String mainActivity;
        public boolean isSystemApp = false;

        public GetInstalled(String appName, boolean hasUpdate, String pkgName, String versionName,
                            int versionCode, int usedTimes, int flag, boolean isSystemApp, String mainActivity) {
            this.appName = appName;
            this.hasUpdate = hasUpdate;
            this.pkgName = pkgName;
            this.versionName = versionName;
            this.versionCode = versionCode;
            this.usedTimes = usedTimes;
            this.flag = flag;
            this.isSystemApp = isSystemApp;
            this.mainActivity = mainActivity;
        }

        // Todo, flag hasUpdate usedTimes
        public GetInstalled(AppInfo appInfo) {
            this.appName = appInfo.appName;
//			this.hasUpdate = appInfo.hasUpdate;
            this.pkgName = appInfo.pkgName;
            this.versionName = appInfo.versionName;
            this.versionCode = appInfo.versionCode;
//			this.usedTimes = appInfo.usedTimes;
//			this.flag = appInfo.flag;
            this.isSystemApp = appInfo.isSystemApp;
            this.mainActivity = appInfo.mainActivity;
        }

    }

    public static class DownTask extends JObject {
        public String appid = "";
        public String appname = "";
        public String apkPath = "";
        public String pkgName = "";

        public DownTask(DownloadTask task) {
            if (task == null) {
                return;
            }
            this.appid = task.ID;
            this.appname = task.title;
            this.apkPath = task.savepath + task.filename;
            try {
                DownloadExtra extra = AppResultBean.parseObject(task.extra,
                        DownloadExtra.class);
                this.pkgName = extra.pkgname;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    public static class GetVersion extends JObject {
        public int versionCode;
    }

    public static class GetAppStatus extends JObject {
        public String pkgName = "";
    }

    public static class StartApp extends JObject {
        public String pkgname = "";
        public String mainActivity = "";
    }


    public static class GetDownLoaded extends JObject {
        public String appId;
        public String appName;
        public String apkPath;
        public String pkgName;
        public String iconUrl;
    }

}
